using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Windows.Input;
using System.Xml.Serialization;
using Microsoft.Win32;
using Samurai.Infrastructure;

namespace Samurai.Client.Commands
{
    public class ExportCommand : ICommand
    {
        private readonly ObservableCollection<Movie> _movies;

        public ExportCommand(ObservableCollection<Movie> movies)
        {
            _movies = movies;
        }

        #region Implementation of ICommand

        public void Execute(object parameter)
        {
            var ls = _movies.ToList();
            var instance = new XmlSerializer(typeof(List<Movie>));

            var dlg = new SaveFileDialog();

            var response = dlg.ShowDialog();

            if (response.Value == true)
            {
                var sr = File.OpenWrite(dlg.FileName);

                instance.Serialize(sr, ls);

                sr.Close();
            }
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

        #endregion
    }
}